package org.newlifecac;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class GetHtml extends Activity implements OnClickListener
{
	private Button cancel = null;
	private Button back = null;
	private WebView browser = null;
	private String html;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.submain);

		back = (Button) findViewById(R.id.back);
		cancel = (Button) findViewById(R.id.cancel);
		browser = (WebView) findViewById(R.id.myWeb1);
		
		back.setOnClickListener(this);
		cancel.setOnClickListener(this);
		
		
		//browser.getSettings().setJavaScriptEnabled(true);
		//browser.getSettings().setPluginsEnabled(true);
		//loadHtml();
		
		browser.requestFocus(); 
		browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
		WebSettings setting = browser.getSettings(); 
		setting.setJavaScriptEnabled(true); 
		setting.setSupportZoom(true); 
		setting.setBuiltInZoomControls(true); 
		setting.setAllowFileAccess(true); 
		setting.setJavaScriptCanOpenWindowsAutomatically(true); 
		setting.setAppCachePath("/data/data/" + getPackageName() + "/app_path/"); 
		setting.setAppCacheEnabled(true); 
		setting.setUserAgent(0); 
		setting.setLoadsImagesAutomatically(true); 
		setting.setSavePassword(true); 
		setting.setLightTouchEnabled(true); 
		setting.setPluginsEnabled(true); 
		setting.setPluginState(PluginState.ON); 
		setting.setUserAgentString("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; zh-tw) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16"); 
		browser.setWebViewClient(new WebViewClient()); 
		html = "<html><body><iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/_KwlZu3HdTM\" rel=\"nofollow\"/>?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>"+"</body></html>";
		
		html = html.replace("?", "%3f"); 
		html = html.replace("%", "%25"); 
		html = html.replace("#", "%23"); 
		html = html.replace("\\", "%27");
		browser.loadData(html, "text/html", "utf-8");
		
	}
	
	
	
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.back:
			System.out.println("back");
			onKeyDown(KeyEvent.KEYCODE_BACK, null);
			break;

		case R.id.cancel:
			System.out.println("cancel");
			break;
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK )
		{
			System.out.println("onKeydown");
			Intent intent = new Intent();
			intent.setClass(GetHtml.this, FilePickerActivity.class);
			startActivity(intent);
			GetHtml.this.finish();
		}
		return false;
	}

	public void loadHtml()
	{
		String strUrl = "www.google.com";
		String strFile = Environment.getExternalStorageDirectory().getPath() + "/"+"tmp.html";
		if (!strUrl.startsWith("http://"))
		{
			strUrl = "http://" + strUrl;
		}
		getStaticPageByBytes(strUrl, strFile);
		browser.loadUrl("file://" + strFile);
	}

	private void getStaticPageByBytes(String surl, String strFile)
	{

		Log.i("getStaticPageByBytes", surl + ", " + strFile);

		HttpURLConnection connection = null;
		InputStream is = null;

		File file = new File(strFile);
		FileOutputStream fos = null;

		try
		{
			URL url = new URL(surl);
			connection = (HttpURLConnection) url.openConnection();

			int code = connection.getResponseCode();
			if (HttpURLConnection.HTTP_OK == code)
			{
				connection.connect();
				is = connection.getInputStream();
				fos = new FileOutputStream(file);

				int i;
				while ((i = is.read()) != -1)
				{
					fos.write(i);
				}

				is.close();
				fos.close();
			}
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if (connection != null)
			{
				connection.disconnect();
			}
		}
	}
}